/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Entity;

/**
 *
 * @author Yihao Li
 */
import java.io.*;
import java.util.*;

public final class FaultyVersion {

    BufferedReader in;
    String dataFileName = "";
    String programName = "";
    int numStmts = 0;
    int numTests = 0;
    int numFaultLocations = -1;
    Vector<Integer> faultLocations = new Vector<Integer>();
    int[][] coverageMatrix;
    int[] resultVector;
    int[] numFailedToExecuteStmt;
    int numFailed = 0;
    int numPassed = 0;
    
    Vector<Rank> tarantulaRankings = new Vector<Rank>();
    Vector<Rank> crosstabRankings = new Vector<Rank>();
    Vector<Rank> ochiaiRankings = new Vector<Rank>();


    public FaultyVersion(String file1) {
        programName = file1;
        dataFileName = file1;
        readData();
        tarantulaRankings = (Vector<Rank>) (Tarantula.getRankings(coverageMatrix, resultVector));
        ochiaiRankings = (Vector<Rank>) (Ochiai.getRankings(coverageMatrix, resultVector));
        crosstabRankings = (Vector<Rank>) (Crosstab.getRankings(coverageMatrix, resultVector));
    }

    public void readData() {
        try {
            in = new BufferedReader(new FileReader(dataFileName));
        } catch (IOException ex) {
            in = null;
            System.out.println(ex.toString());
        }

        if (in != null) {
            String myLine;
            try {
                myLine = in.readLine();

                StringTokenizer st1 = new StringTokenizer(myLine.trim());
                st1.nextToken();

                programName = st1.nextToken();

                myLine = in.readLine();
                st1 = new StringTokenizer(myLine.trim());
                st1.nextToken();

                numStmts = Integer.parseInt(st1.nextToken());

                myLine = in.readLine();
                st1 = new StringTokenizer(myLine.trim());
                st1.nextToken();

                numTests = Integer.parseInt(st1.nextToken());


                coverageMatrix = new int[numTests][numStmts];
                resultVector = new int[numTests];
                numFailedToExecuteStmt = new int[numStmts];

                for (int i = 0; i < numTests; i++) {
                    resultVector[i] = -1;

                    for (int j = 0; j < numStmts; j++) {
                        coverageMatrix[i][j] = -1;
                    }
                }

                for (int i = 0; i < numStmts; i++) {
                    numFailedToExecuteStmt[i] = 0;
                }

                myLine = in.readLine();
                st1 = new StringTokenizer(myLine.trim());
                st1.nextToken();

                numFaultLocations = Integer.parseInt(st1.nextToken());

                myLine = in.readLine();

                if (myLine.trim().length() > 0) {
                    st1 = new StringTokenizer(myLine.trim());
                    st1.nextToken();

                    while (st1.hasMoreTokens()) {
                        String tmp2 = st1.nextToken();
                        faultLocations.add(Integer.parseInt(tmp2));
                    }
                }

                int stmtCounter, testCounter = 0;
                while ((myLine = in.readLine()) != null) {
                    stmtCounter = 0;

                    if (myLine.trim().length() > 0) {
                        StringTokenizer st = new StringTokenizer(myLine.trim(), "#");
                        st.nextToken();
                        st.nextToken();

                        String testCase = st.nextToken();

                        if (testCase.length() == 2) {
                            int result = Integer.parseInt(testCase.charAt(1) + "");
                            resultVector[testCounter] = result;

                            if (result == 0) {
                                numPassed++;
                            } else if (result > 0) {
                                numFailed++;
                            }
                        }

                        String coverageString = st.nextToken();

                        st1 = new StringTokenizer(coverageString);

                        while (st1.hasMoreTokens()) {
                            coverageMatrix[testCounter][stmtCounter] = Integer.parseInt(st1.nextToken());

                            if (coverageMatrix[testCounter][stmtCounter] > 0) {
                                numFailedToExecuteStmt[stmtCounter] += resultVector[testCounter];
                            }

                            stmtCounter++;

                        }
                        testCounter++;
                    }
                }

                in.close();
            } catch (IOException ex) {
                System.out.println(ex.toString());
            }
        }
    }

    public int getBest(Vector v) {
        int best = -10000;
        Double mySusp = -10000.0;

        for (int i = 0; i < v.size(); i++) {
            Rank r = (Rank) (v.elementAt(i));

            if (faultLocations.contains(r.stmt)) {
                mySusp = r.susp;
                break;
            }
        }

        for (int i = 0; i < v.size(); i++) {
            Rank r = (Rank) (v.elementAt(i));

            if (r.susp.compareTo(mySusp) == 0) {
                best = (i + 1);
                System.out.println(best);
                return best;
            }
        }

        return best;
    }

    public int getWorst(Vector v) {
        int worst = -10000;
        Double mySusp = -10000.0;

        for (int i = 0; i < v.size(); i++) {
            Rank r = (Rank) (v.elementAt(i));

            if (faultLocations.contains(r.stmt)) {
                mySusp = r.susp;
                break;
            }
        }

        for (int i = 0; i < v.size(); i++) {
            Rank r = (Rank) (v.elementAt(i));

            if (r.susp.compareTo(mySusp) == 0) {
                worst = (i + 1);
            }
        }
        return worst;
    }
}
